package com.itstyle.seckill.topic.jichu;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;

/**
 * @Auther: liuwenxiang
 * @Date: 2020/4/30 16:27
 * @Description:
 */
public class ListTest {

    private static List<String> arrayList = Collections.synchronizedList(new ArrayList<String>());
    private static List<String> copyOnWriteArrayList = new CopyOnWriteArrayList<String>();
    private static CountDownLatch cdl1 = new CountDownLatch(2);
    private static CountDownLatch cdl2 = new CountDownLatch(2);
    private static CountDownLatch cdl3 = new CountDownLatch(2);
    private static CountDownLatch cdl4 = new CountDownLatch(2);
    //ArrayList写线程
    static class ArrayAddThread extends Thread{

        @Override
        public void run() {
            // TODO Auto-generated method stub
            for(int i=0;i<50000;i++) {
                arrayList.add(String.valueOf(i));
            }
            cdl1.countDown();
        }

    }
    //ArrayList读线程
    static class ArrayGetThread extends Thread{

        @Override
        public void run() {
            // TODO Auto-generated method stub
            int size = arrayList.size();
            for(int i=0;i<size;i++) {
                arrayList.get(i);
            }
            cdl2.countDown();
        }

    }
    //CopyOnWriteArrayList写线程
    static class CopyAddThread extends Thread{

        @Override
        public void run() {
            // TODO Auto-generated method stub
            for(int i=0;i<50000;i++) {
                copyOnWriteArrayList.add(String.valueOf(i));
            }
            cdl3.countDown();
        }

    }

    //CopyOnWriteArrayList写线程
    static class CopyGetThread extends Thread{

        @Override
        public void run() {
            // TODO Auto-generated method stub
            int size = copyOnWriteArrayList.size();
            for(int i=0;i<size;i++) {
                copyOnWriteArrayList.get(i);
            }
            cdl4.countDown();
        }

    }

    public static void main(String[] args) throws InterruptedException {
        long start1 = System.currentTimeMillis();
        new ArrayAddThread().start();
        new ArrayAddThread().start();
        cdl1.await();
        long end1 = System.currentTimeMillis();
        System.out.println("ArrayList写操作时间："+(end1-start1));

        long start3 = System.currentTimeMillis();
        new CopyAddThread().start();
        new CopyAddThread().start();
        cdl3.await();
        long end3 = System.currentTimeMillis();
        System.out.println("CopyOnWriteArrayList的写操作时间："+(end3-start3));

        long start2 = System.currentTimeMillis();
        new ArrayGetThread().start();
        new ArrayGetThread().start();
        cdl2.await();
        long end2 = System.currentTimeMillis();
        System.out.println("ArrayList读操作时间："+(end2-start2));


        long start4 = System.currentTimeMillis();
        new CopyGetThread().start();
        new CopyGetThread().start();
        cdl4.await();
        long end4 = System.currentTimeMillis();
        System.out.println("CopyOnWriteArrayList的读操作时间："+(end4-start4));


    }

}
